﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace TS_XY_Train_Repaire.Pub.ChartForm
{
    public partial class Pub_ChartForm_MainForm_FirstPage : DevExpress.XtraEditors.XtraForm
    {
        public Pub_ChartForm_MainForm_FirstPage()
        {
            InitializeComponent();
        }
        private void Pub_ChartForm_MainForm_FirstPage_Load(object sender, EventArgs e)
        {
            _Load_CC_Stuff_Dep();
            _Load_CC_Plane_Train();
            _LoadMessage_List();
        }

        private void Pub_ChartForm_MainForm_FirstPage_FormClosing(object sender, FormClosingEventArgs e)
        {
            TS_Win_Locomotive_MainForm.htActionItem.Remove(this.Name);
        }
        #region//--系统信息事件--//
        private void bt_Message_Sign_Click(object sender, EventArgs e)
        {
            if (!cb_Message_Sign.Checked)
            {
                string strSQL = "";
                for (int i = 0; i < gv_Voice.SelectedRowsCount; i++)
                {
                    strSQL = "update TS_S_T_P_Voice_Announce set f_stuffid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_stime=getdate() where f_id=" + gv_Voice.GetRowCellValue(i, "f_id").ToString();
                    TS_Win_Class.MySqlCom(strSQL);
                    try
                    {
                        if (TS_Win_Class.GetSQlFieldValue("select (select f_state from TS_S_T_S_RP_DataState where f_id=" + gv_Voice.GetRowCellValue(i, "f_tablename").ToString() + ".f_state) as f_state from " + gv_Voice.GetRowCellValue(i, "f_tablename").ToString() + "  where f_id=" + gv_Voice.GetRowCellValue(i, "f_vid").ToString(), "f_state") == "9")
                        {
                        }
                        else
                        {
                            strSQL = "update " + gv_Voice.GetRowCellValue(i, "f_tablename").ToString() + " set f_stuffid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_stime=getdate(),f_state=1 where f_id=" + gv_Voice.GetRowCellValue(i, "f_vid").ToString() + " and f_state=0";
                            TS_Win_Class.MySqlCom(strSQL);
                        }
                        if (gv_Voice.GetRowCellValue(i, "f_tablename").ToString() == "TS_TR_BU_Work_Main_Relation")
                        {
                            string[] _str = TS_Win_Class.GetSQlFieldValue("select f_rid,f_rtype from "+ gv_Voice.GetRowCellValue(i, "f_tablename").ToString()+" where f_id="+ gv_Voice.GetRowCellValue(i, "f_vid").ToString(), new string[] { "f_rid","f_rtype"});
                            switch(_str[1])
                            {
                                case "0":
                                    strSQL = "update TS_TR_BU_Work_Main set f_state=2 where f_id=" + _str[0] + " and f_state=1";
                                    TS_Win_Class.MySqlCom(strSQL);
                                    break;
                                case "1":
                                    strSQL = "update TS_TR_BU_Work_Main_Day set f_state=2 where f_id=" + _str[0] + " and f_state=1";
                                    TS_Win_Class.MySqlCom(strSQL);
                                    break;
                                case "2":
                                    strSQL = "update TS_TR_BU_Work_Main_Steps set f_state=2 where f_id=" + _str[0] + " and f_state=1";
                                    TS_Win_Class.MySqlCom(strSQL);
                                    break;
                                case "3":
                                    strSQL = "update TS_TR_BU_Work_Main_Group set f_state=2 where f_id=" + _str[0] + " and f_state=1";
                                    //strSQL += "update TS_TR_BU_Work_Main_Group_WorkPlace set f_state=2 where f_bmwgid=" + _str[0] + " and f_state=1";
                                    TS_Win_Class.MySqlCom(strSQL);
                                    break;
                                default:
                                    strSQL = "update TS_TR_BU_Work_Main_Group_WorkPlace set f_state=2 where f_id=" + _str[0] + " and f_state=1";
                                    TS_Win_Class.MySqlCom(strSQL);
                                    break;
                            }
                        }
                    }
                    catch
                    {
                    }
                }
                _LoadMessage_List();
            }
        }

        private void bt_Message_Sign_All_Click(object sender, EventArgs e)
        {

        }

        private void cb_Message_Sign_CheckedChanged(object sender, EventArgs e)
        {
            if (cb_Message_Sign.Checked)
            {
                cb_Message_Sign.Text = "已签收信息";
            }
            else
            {
                cb_Message_Sign.Text = "未签收信息";
            }
            _LoadMessage_List();
        }
        private void cb_Message_Voice_CheckedChanged(object sender, EventArgs e)
        {
            if (cb_Message_Voice.Checked)
            {
                cb_Message_Voice.Text = "语音提醒";
            }
            else
            {
                cb_Message_Voice.Text = "无语音提醒";
            }
        }
        private void cb_Message_Refresh_CheckedChanged(object sender, EventArgs e)
        {
            if (cb_Message_Refresh.Checked)
            {
                cb_Message_Refresh.Text = "自动刷新";
                tm_Message.Enabled = true;
            }
            else
            {
                cb_Message_Refresh.Text = "手动刷新";
                tm_Message.Enabled = false;
            }
            _LoadMessage_List();
        }
        private void bt_Message_Query_Click(object sender, EventArgs e)
        {
            if (tb_Message_Query.Text.Trim().Length>0)
            {
                cb_Message_Refresh.Checked = false;
            }
            else
            {

            }
        }
        private void tm_Message_Tick(object sender, EventArgs e)
        {
            _LoadMessage_List();
        }
        private void _LoadMessage_List()
        {
            DataTable _dt = new DataTable();
            string strSQL = "select *,(case f_type when 0 then '个人' when 1 then '职务' when 2 then '部门' end) as f_typename,(case f_type when 0 then (select f_name from TS_S_T_P_Stuff where f_id=TS_S_T_P_Voice_Announce.f_funcid) when 1 then (select f_name from TS_S_T_P_Position where f_id=TS_S_T_P_Voice_Announce.f_funcid) else (select f_name from TS_S_T_P_Department where f_id=TS_S_T_P_Voice_Announce.f_funcid) end) as f_funcname,(case f_stuffid when -1 then '未签收' else (select f_name from TS_S_T_P_Stuff where f_id=TS_S_T_P_Voice_Announce.f_stuffid ) end) as f_stuffname,(case f_workerid when -1 then '未签收' else (select f_name from TS_S_T_P_Stuff where f_id=TS_S_T_P_Voice_Announce.f_workerid ) end) as f_workername from TS_S_T_P_Voice_Announce where f_del=0 and f_stuffid<=0 and ((f_type=0 and f_funcid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ") or (f_type=1 and f_funcid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.UserPosID + ") or (f_type=2 and f_funcid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.UserDepIndex + ")) order by f_time desc";
            if (cb_Message_Sign.Checked)
            {
                strSQL = "select *,(case f_type when 0 then '个人' when 1 then '职务' when 2 then '部门' end) as f_typename,(case f_type when 0 then (select f_name from TS_S_T_P_Stuff where f_id=TS_S_T_P_Voice_Announce.f_funcid) when 1 then (select f_name from TS_S_T_P_Position where f_id=TS_S_T_P_Voice_Announce.f_funcid) else (select f_name from TS_S_T_P_Department where f_id=TS_S_T_P_Voice_Announce.f_funcid) end) as f_funcname,(case f_stuffid when -1 then '未签收' else (select f_name from TS_S_T_P_Stuff where f_id=TS_S_T_P_Voice_Announce.f_stuffid ) end) as f_stuffname,(case f_workerid when -1 then '未签收' else (select f_name from TS_S_T_P_Stuff where f_id=TS_S_T_P_Voice_Announce.f_workerid ) end) as f_workername from TS_S_T_P_Voice_Announce where f_del=0 and f_stuffid>-1 and ((f_type=0 and f_funcid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ") or (f_type=1 and f_funcid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.UserPosID + ") or (f_type=2 and f_funcid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.UserDepIndex + ")) order by f_time desc";
            }
            else
            {
                strSQL = "select *,(case f_type when 0 then '个人' when 1 then '职务' when 2 then '部门' end) as f_typename,(case f_type when 0 then (select f_name from TS_S_T_P_Stuff where f_id=TS_S_T_P_Voice_Announce.f_funcid) when 1 then (select f_name from TS_S_T_P_Position where f_id=TS_S_T_P_Voice_Announce.f_funcid) else (select f_name from TS_S_T_P_Department where f_id=TS_S_T_P_Voice_Announce.f_funcid) end) as f_funcname,(case f_stuffid when -1 then '未签收' else (select f_name from TS_S_T_P_Stuff where f_id=TS_S_T_P_Voice_Announce.f_stuffid ) end) as f_stuffname,(case f_workerid when -1 then '未签收' else (select f_name from TS_S_T_P_Stuff where f_id=TS_S_T_P_Voice_Announce.f_workerid ) end) as f_workername from TS_S_T_P_Voice_Announce where f_del=0 and f_stuffid<0 and ((f_type=0 and f_funcid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ") or (f_type=1 and f_funcid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.UserPosID + ") or (f_type=2 and f_funcid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.UserDepIndex + ")) order by f_time desc";
            }
            TS_Win_Class.FilledDataTable(strSQL, _dt);
            gc_Voice.DataSource = _dt;
        }
        #endregion
        #region//--人员统计信息--//
        private void _Load_CC_Stuff_Dep()
        {
            cc_Stuff_Dep.Series[0].Points.Clear();
            string strSQL = "select f_name,(select count(*) from TS_S_T_P_Stuff where f_del=0 and (f_depid=a.f_id or f_depid in (select f_id from TS_S_T_P_Department where f_del=0 and f_level like '%,'+convert(nvarchar(20),a.f_id)+',%'))) as f_count from TS_S_T_P_Department as a where f_del=0 and f_deep=0 and f_rsid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.UserRSID;
            SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn.Open();
            SqlCommand cmd = new SqlCommand(strSQL, conn);
            SqlDataReader read = cmd.ExecuteReader();
            while (read.Read())
            {
                cc_Stuff_Dep.Series[0].Points.Add(new DevExpress.XtraCharts.SeriesPoint(read["f_name"].ToString(), new object[] { ((object)((int)read["f_count"])) }));
            }
            read.Close();
            conn.Close();
            conn.Dispose();
        }
        #endregion
        #region//--计划兑现情况图表--//
        private void _Load_CC_Plane_Train()
        {
            cc_Plane_Train.Series[0].Points.Clear();
            cc_Plane_Train.Series[1].Points.Clear();
            cc_Plane_Train.Series[2].Points.Clear();
            cc_Plane_Train.Series[3].Points.Clear();
            string strSQL = "select f_monthname as f_name,count(*) as f_count from TS_TR_PS_Loco_Repair_Plane_Train where f_del=0 and f_mainid in (select f_id from TS_TR_PS_Loco_Repair_Plane_Main where f_del=0 and f_type=0 and f_state=0 and datediff(year,getdate(),f_stime)=0) group by f_monthname order by f_monthname asc";
            SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn.Open();
            SqlCommand cmd = new SqlCommand(strSQL, conn);
            SqlDataReader read = cmd.ExecuteReader();
            while (read.Read())
            {
                cc_Plane_Train.Series[0].Points.Add(new DevExpress.XtraCharts.SeriesPoint(read["f_name"].ToString()+"月份", new object[] { ((object)((int)read["f_count"])) }));
            }
            strSQL = "select month(f_itime) as f_name,count(*) as f_count from TS_TR_BU_Work_Main where f_del=0 and datediff(year,getdate(),f_itime)=0 and f_state in (select f_id from TS_S_T_S_RP_DataState where f_del=0 and f_state not in (-1)) group by month(f_itime)  order by month(f_itime)  asc";
            read.Close();
            cmd = new SqlCommand(strSQL, conn);
            read = cmd.ExecuteReader();
            while (read.Read())
            {
                cc_Plane_Train.Series[1].Points.Add(new DevExpress.XtraCharts.SeriesPoint(read["f_name"].ToString() + "月份", new object[] { ((object)((int)read["f_count"])) }));
            }
            strSQL = "select month(f_stime) as f_name,count(*) as f_count from TS_TR_BU_Work_Main where f_del=0 and datediff(year,getdate(),f_stime)=0 and f_state in (select f_id from TS_S_T_S_RP_DataState where f_del=0 and f_state not in (-1)) group by month(f_stime)  order by month(f_stime)  asc";
            read.Close();
            cmd = new SqlCommand(strSQL, conn);
            read = cmd.ExecuteReader();
            while (read.Read())
            {
                cc_Plane_Train.Series[2].Points.Add(new DevExpress.XtraCharts.SeriesPoint(read["f_name"].ToString() + "月份", new object[] { ((object)((int)read["f_count"])) }));
            }
            strSQL = "select month(f_etime) as f_name,count(*) as f_count from TS_TR_BU_Work_Main where f_del=0 and datediff(year,getdate(),f_etime)=0 and f_state in (select f_id from TS_S_T_S_RP_DataState where f_del=0 and f_state not in (-1)) group by month(f_etime)  order by month(f_etime)  asc";
            read.Close();
            cmd = new SqlCommand(strSQL, conn);
            read = cmd.ExecuteReader();
            while (read.Read())
            {
                cc_Plane_Train.Series[3].Points.Add(new DevExpress.XtraCharts.SeriesPoint(read["f_name"].ToString() + "月份", new object[] { ((object)((int)read["f_count"])) }));
            }
            read.Close();
            conn.Close();
            conn.Dispose();
        }
        #endregion

    }
}